package com.jzo2o.foundations.service.impl;

/**
 * @Description ServeService
 * @Author yiboliu
 * @Date 2024-12-01
 */

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 服务表，服务实现类
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {


    /**
     * 查询区域服务分页列表
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        //多表关联分页查询时，应该使用PageHelper工具类完成
        return PageHelperUtils.selectPage(dto,()->baseMapper.findServeListByRegionId(dto.getRegionId()));
    }

    @Autowired
    private IServeItemService serveItemService;

    @Autowired
    private IRegionService regionService;

    /**
     * 添加服务
     *
     * @param dtoList
     */
    @Override
    public void batchAdd(List<ServeUpsertReqDTO> dtoList) {

        for (ServeUpsertReqDTO dto : dtoList) {
            Long itemId = dto.getServeItemId();

            //1. 判断服务项是否存在
            ServeItem item = serveItemService.getById(dto.getServeItemId());
            if (item==null){
                throw new ForbiddenOperationException("服务不存在");
            }

            //2. 判断服务项是否已启用
            if (item.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
                throw new ForbiddenOperationException("服务未启用");
            }

            //3. 判断服务项是否已添加过
            Integer count = this.lambdaQuery()
                    .eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .count();

            if (count>0){
                throw new ForbiddenOperationException("服务项已经添加");
            }
            //4. 构建服务对象数据
            //复制服务项为服务信息
            Serve serve= BeanUtils.copyBean(dto,Serve.class);
            //查询区域信息
            Region region=regionService.getById(dto.getRegionId());
            serve.setCityCode(region.getCityCode());
            //5. 添加服务数据到表中
            boolean result = this.save(serve);
            if (!result){
                throw new DBException("添加服务失败");
            }

        }
    }

    /**
     * 更改价格
     * @param id
     * @param price
     */
    @Transactional//事务管理
    @Override
    public void updatePrice(Long id, BigDecimal price) {
        //1，判断服务是否存在
        Serve serve = this.getById(id);
        if (serve==null){
            throw new ForbiddenOperationException("服务不存在");
        }
        //2.根据ID更新价格
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();
        if (!result){
            throw new DBException("更新价格失败");
        }
    }

    /**
     * 上架
     * @param id
     */
    @Transactional
    @Override
    public void onSale(Long id) {
        //1,判断服务是否存在
        Serve serve = this.getById(id);
        if (serve==null){
            throw new ForbiddenOperationException("服务不存在");
        }
        //2,判断是否有区域
        if (serve.getRegionId()==null){
            throw new ForbiddenOperationException("未选择区域");
        }
        //3,判断服务是否上架过
        if (serve.getSaleStatus()==FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务已上架");
        }
        //4,判断服务项是否存在
        ServeItem item = serveItemService.getById(id);
        if (item==null){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //5,判断服务项是否已启动
        if (item.getActiveStatus()!=FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项未启动");
        }
        //6,根据id修改状态为已上架
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if (!result){
            throw new DBException("服务上架失败");
        }
    }

    /**
     * 删除区域服务
     * @param id
     */
    @Override
    public void detele(Long id) {
        //1，判断服务是否存在
        Serve serve = this.getById(id);
        if (serve==null){
            throw new ForbiddenOperationException("服务不存在");
        }
        //2，判断是否草稿
        if (serve.getSaleStatus()!=FoundationStatusEnum.INIT.getStatus()){
            throw new ForbiddenOperationException("不是草稿");
        }
        //3,
        boolean result = this.removeById(id);
        if (!result){
            throw new DBException("删除服务失败");
        }
    }

    /**
     * 区域服务下架
     * @param id
     */
    @Override
    public void offSale(Long id) {
        //1,判断服务是否存在
        Serve serve = this.getById(id);
        if (serve==null){
            throw new ForbiddenOperationException("服务不存在");
        }
        //2,判断服务是否已下架
        if (serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务已下架");
        }
        //3，判断服务项是否存在
        ServeItem item = serveItemService.getById(id);
        if (item==null){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //4,执行下架操作
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if (!result){
            throw new DBException("下架服务失败");
        }
    }

    private static final int IS_HOT = 1;
    private static final int NO_HOT = 0;

    /**
     * 区域服务设置热门
     * @param id
     */
    @Override
    public void onHot(Long id) {
        //1,判断服务是否存在
        Serve serve = this.getById(id);
        if (serve==null){
            throw new ForbiddenOperationException("服务不存在");
        }
        //2，判断服务项是否存在
        ServeItem item = serveItemService.getById(serve.getServeItemId());
        if (item==null){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //3，判断服务的状态
        if (serve.getIsHot()==IS_HOT){
            throw new ForbiddenOperationException("服务已在热门状态");
        }
        //4，修改热门
        boolean result = this.lambdaUpdate()
                .set(Serve::getIsHot, IS_HOT)
                .eq(Serve::getId, id)
                .update();
        if(!result){
            throw new DBException("修改服务热门失败");
        }
    }

    @Autowired
    private ServeItemMapper serveItemMapper;
    /**
     * 取消热门服务
     *
     * @param id
     */
    @Override
    public void offHot(Long id) {
        Serve serve = this.getById(id);
        //判断服务是否存在
        if (serve == null) {
            throw new ForbiddenOperationException("服务不存在");
        }
        //判断服务项是否存在
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (serveItem == null) {
            throw new ForbiddenOperationException("服务项不存在");
        }
        //判断服务的状态
        if (serve.getIsHot() == NO_HOT) {
            throw new ForbiddenOperationException("服务已经不是热门");
        }
        //修改热门
        boolean result = this.lambdaUpdate()
                .set(Serve::getIsHot, NO_HOT)
                .eq(Serve::getId, id)
                .update();
        if (!result) {
            throw new DBException("修改服务不热门失败");
        }
    }
}
